package com.msb.mall.product.controller;

import com.msb.common.utils.PageUtils;
import com.msb.common.utils.R;
import com.msb.mall.product.entity.vo.AttrResponseVo;
import com.msb.mall.product.entity.vo.AttrVo;
import com.msb.mall.product.service.AttrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;




/**
 * 商品属性
 *
 * @author lwq
 * @email 1907326957@qq.com
 * @date 2024-06-12 21:28:32
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;


    /**
     * 列表
     * 分页查询
     * 前端请求需要封装对应的分页数据
     * {
     *     page: 1 //当前页
     *     limit: 10 //每页显示的条数
     *     sidx: "id" //排序的字段
     *     order: "asc/desc" //排序的方式
     *     key: "小米" //查询的关键字
     * }
     */
    @RequestMapping("/{attrType}/list/{catId}")
    public R queryBastPage(@RequestParam Map<String, Object> params,
                  @PathVariable("catId") Long catId,
                  @PathVariable("attrType") String attrType){
        params.put("catId",catId);
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrService.queryBastPage(params,catId,attrType);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
//		AttrEntity attr = attrService.getById(attrId);
        AttrResponseVo attr = attrService.getAttrInfo(attrId);
        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVo attr){
		//attrService.save(attr);
        attrService.saveAttr(attr);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVo attr){
//		attrService.updateById(attr);
        attrService.updateBaseAttr(attr);
        return R.ok();
    }

    /**
     * 删除
     * 基本属性删除：需要将关联的属性组也要删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
//		attrService.removeByIds(Arrays.asList(attrIds));
        attrService.removeByIdsDetails(Arrays.asList(attrIds));
        return R.ok();
    }

}
